package com.zmn.oms.admin.controller.work.modify;

import com.google.common.collect.Lists;
import com.zmn.cube.common.utils.StringUtil;
import com.zmn.oms.admin.controller.work.WorkBaseController;
import com.zmn.oms.business.interfaces.xno.OrderTempXnoBindBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderXnoConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.common.utils.MobileUtil;
import com.zmn.oms.model.dto.work.xno.TempXnoBindDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.model.entity.xno.OrderXnoBindQuery;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.services.interfaces.xno.OrderXnoService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 类描述：小号
 *
 * @author ligang
 * @date 2022/01/10 16:54
 */
@Slf4j
@Controller
@RequestMapping("/work/modify")
public class WorkXnoController extends WorkBaseController {

    @Resource
    private OrderTempXnoBindBService orderTempXnoBindBService;
    @Resource
    private OrderXnoService orderXnoService;
    @Resource
    private ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;

    /**
     * 临时小号
     */
    @RequestMapping(value = "/tempXno")
    public ModelAndView tempXno(Long orderId, Long workId) {
        ModelAndView mv = new ModelAndView("work/modify/orderTempXno");
        // 订单详情
        OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(orderId);
        List<String> userTelephoneList = Lists.newArrayList();
        if (StringUtils.isNotBlank(orderDetail.getTelephone())) {
            userTelephoneList.add(MobileUtil.formatHideTel(orderDetail.getTelephone()));
        }
        if (StringUtils.isNotBlank(orderDetail.getTelephone2())) {
            userTelephoneList.add(MobileUtil.formatHideTel(orderDetail.getTelephone2()));
        }
        if (StringUtils.isNotBlank(orderDetail.getTelephone3())) {
            userTelephoneList.add(MobileUtil.formatHideTel(orderDetail.getTelephone3()));
        }

        mv.addObject("userTelephoneList", userTelephoneList);
        mv.addObject("orderId", orderId);
        mv.addObject("workId", workId);
        return mv;
    }

    /**
     * 保存临时小号
     */
    @ResponseBody
    @RequestMapping(value = "/save/tempXno")
    public ResultDTO saveTempXno(@RequestBody TempXnoBindDTO tempXnoBindDTO) {
        try {
            Assert.notNull(tempXnoBindDTO.getBindTelephone(), "绑定的业务员手机号不能为空");
            Assert.notNull(tempXnoBindDTO.getBindTelephone(), "绑定用户电话不能为空");
            OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(
                    tempXnoBindDTO.getOrderId(), tempXnoBindDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_ORDER_TEMP_XNO);
            if (!operatePermission.getCanOperate()) {
                return ResultDTO.fail(operatePermission.getMsg());
            }
            OrderWork orderWork = orderWorkService.findOrderWorkByKey(tempXnoBindDTO.getOrderId(), tempXnoBindDTO.getOrderId());
            if (Objects.isNull(orderWork)) {
                throw new OmsBaseException("无效的工单");
            }
            // 是否存在绑定
            Boolean bindStatus = orderTempXnoBindBService.queryTempXnoBindStatus(orderWork, tempXnoBindDTO.getBindTelephone(), tempXnoBindDTO.getUserTelephone());
            if (bindStatus) {
                return ResultDTO.fail("小号绑定中，不可操作");
            }
            // 绑定数量限制
            OrderXnoBindQuery orderXnoBindQuery = OrderXnoBindQuery
                    .builder().workId(tempXnoBindDTO.getWorkId())
                    .bindStatus(OrderConsts.BIND_STATUS_BIND)
                    .bindType(OrderXnoConsts.BIND_TYPE_TEMP).build();
            Integer count = orderXnoService.countXnoBindByQuery(orderXnoBindQuery);
            if (count >= 3) {
                return ResultDTO.fail("临时小号数量超限，不可操作");
            }
            tempXnoBindDTO.setOperator(super.getCurrentStaff().getRealName());
            orderTempXnoBindBService.bindTempXno(tempXnoBindDTO, orderWork);
        } catch (Exception e) {
            return ResultDTO.fail(e.getMessage());
        }
        return ResultDTO.success();
    }
}

